package com.liuxinlong.common.uploadSSH;

import ch.ethz.ssh2.*;
import ch.ethz.ssh2.Session;
import com.liuxinlong.common.AosException;
import com.liuxinlong.enums.ReturnCodes;
import com.jcraft.jsch.*;

import java.io.*;
import java.util.Properties;


public class UploadDownloadSSHUtil {


    public void connnect(String remoteUrl) {
        Connection conn = null;
        Session session = null;
        try {
            conn = new Connection(remoteUrl);
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword("pdmadmin@hotwater.com.cn","pA2016!");
            if (!isAuthenticated) {
                throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "登陆远程服务器失败");
            }

            // 执行命令
            session = conn.openSession();
            session.execCommand("df -h");
            InputStream stdout = new StreamGobbler(session.getStdout());
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);

            }
            br.close();

            // 下载文件到本地
            SCPClient scpClient = conn.createSCPClient();
            SCPInputStream scpis = scpClient.get("D:/pmip_PDF");
            FileOutputStream fos = new FileOutputStream("D:/test");
            byte[] buffer = new byte[1024];
            int len = 0;
            while((len=scpis.read(buffer)) != -1) {
                fos.write(buffer,0,len);
            }
            fos.close();

            // SFTP
            SFTPv3Client sftpClient = new SFTPv3Client(conn);
            sftpClient.createFile("D:/pmip_PDF/abc.txt");
            sftpClient.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            session.close();
            conn.close();
        }
    }

    /**
     * Description: 从目录拷贝文件到本地
     *
     * @param remoteUrl 目录上的文件路径
     * @param localDir  本地目录
     */
    public void download(String remoteUrl, String localDir) {
        String user = "";
        String host = "";
        int port = 22;
        String password = "";
        String remotePath = "";
        String localPath = "";

        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = null;

        try {
            session = jsch.getSession(user,host,port);
            session.setPassword(password);
            Properties props = new Properties();
            props.setProperty("StrictHostKeyChecking","on");
            session.setConfig(props);
            session.connect();
            ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
            channel.get(remotePath, localPath, new SftpProgressMonitor() {
                @Override
                public void init(int i, String s, String s1, long l) {
                    System.out.println("download start ...");
                }

                @Override
                public boolean count(long l) {
                    System.out.println(l);
                    return false;
                }

                @Override
                public void end() {
                    System.out.println("download complete!");
                }
            });

        } catch (JSchException e) {
            e.printStackTrace();
        } catch (SftpException e) {
            e.printStackTrace();
        }finally {
          session.disconnect();
        }
    }


}